package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.qhse.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.qhse.ContingencyDrillMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.qhse.ContingencyDrill;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.qhse.ContingencyDrillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.impl.qhse
 * @Description: ${TODO}(应急演练服务层具体实现)
 * @author: lutianbo
 * @date: 2016/11/29,0029
 */
public class ContingencyDrillServiceImpl implements ContingencyDrillService{
    @Autowired
    private ContingencyDrillMapper contingencyDrillMapper;
    @Autowired
    private IamFileService iamFileService;

    /**
     *根据ids删除应急演练
     * @param contingencyDrillIds
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] contingencyDrillIds){
        int num=0;
        if(contingencyDrillIds.length > 0){
            for(int i=0;i<contingencyDrillIds.length;i++){
                num += contingencyDrillMapper.deleteByPrimaryKey(contingencyDrillIds[i]);
            }
        }
        return num;
    }

    /**
     * 新增应急演练
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles hbase中需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(ContingencyDrill record,String uploadFiles,String deletedFiles){
        // 插入数据成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return resultCount;
        }
        // 保存业务数据
        resultCount += contingencyDrillMapper.insertSelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getContingencyDrillId());

        return resultCount;
    }

    /**
     * 根据id查询
     * @param contingencyDrillId
     * @return
     */
    @Override
    public ContingencyDrill selectByPrimaryKey(String contingencyDrillId){
        if(!"".equals(contingencyDrillId)){
            ContingencyDrill drill = contingencyDrillMapper.selectByPrimaryKey(contingencyDrillId);
            return  drill;
        }
       return null;
    }

    /**
     * 根据id更新应急演练
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles hbase中需要删除的文件
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(ContingencyDrill record,String uploadFiles,String deletedFiles){
        // 插入数据成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return resultCount;
        }
        resultCount +=  contingencyDrillMapper.updateByPrimaryKeySelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getContingencyDrillId());

        return resultCount;
    }

    /**
     * 查询应急演练列表
     * @param searchBean
     * @return
     */
    @Override
    public JSONObject contingencyDrillList(BootstrapSearchBean searchBean){
        // 返回结果集
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        searchBean.setLimit( searchBean.getLimit() == null ? Integer.valueOf(10) : searchBean.getLimit() );
        searchBean.setOffset( searchBean.getOffset() == null ? Integer.valueOf(0) : searchBean.getOffset() );
        // 总条数
        jsonObject.put("total",contingencyDrillMapper.queryCount(searchBean));
        // 信息
        jsonObject.put("rows", JSON.toJSON(contingencyDrillMapper.queryList(searchBean)));
        return jsonObject;
    }

    /**
     * 判断应急演练名是否存在
     * @param drillName 应急演练名
     * @return ture:存在 false:不存在
     */
    @Override
    public boolean isExitByName(String drillName) {
        return contingencyDrillMapper.isExitByName(drillName) > 0 ? true :false ;
    }
}
